package com.zhaoc.service.impl;

import com.zhaoc.dao.CustomerRepository;
import com.zhaoc.domain.Customer;
import com.zhaoc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>title:com.zhaoc.service.impl</p>
 * date:2018/8/4 0004
 * time:18:50
 * author:赵成
 * user:13566
 */
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {

    //注入dao层对象
    @Autowired
    private CustomerRepository customerRepository;

    //查询所有未关联定区的客户
    @Override
    public List<Customer> findNoAssociationCustomers() {
        return customerRepository.findByFixedAreaIdIsNull();
    }

    //根据定区ID查询已关联该定区的客户
    @Override
    public List<Customer> findHasAssociationFixedareaCustomers(String fixedAreaId) {
        return customerRepository.findByFixedAreaId(fixedAreaId);
    }

    //将该客户绑定到该定区
    @Override
    public void associationCustomersToFixedarea(String customersIdStu, String fixedAreaId) {

        //解除关联动作
        customerRepository.claerFixedAreaId(fixedAreaId);

        //切割字符串
        String[] coustomerIdArray = customersIdStu.split(",");
        for (String customerId:coustomerIdArray) {
            Integer id = Integer.parseInt(customerId);
            customerRepository.updateFixedAreaId(id, fixedAreaId);
        }
    }

    //保存客户信息
    @Override
    public void saveCustomer(Customer customer) {
        customerRepository.save(customer);
    }

    //查询客户信息
    @Override
    public Customer findCustomer(String telephone) {
        return customerRepository.findBytelephone(telephone);
    }

    //修改邮箱激活状态
    @Override
    public void updateCustomerType(String telephone) {
        customerRepository.updateTypeByTelephone(telephone);
    }

    //用户登陆验证
    @Override
    public Customer login(String telephone, String password) {
        return customerRepository.findByTelephoneAndPassword(telephone,password);
    }

    //根据用户下单的详细地址查询定区编号
    @Override
    public String findFixedAreaIdByAddress(String address) {
        return customerRepository.findFixedAreaIdByAddress(address);
    }
}
